package org.eclipse.snippets.code;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.*;

public class Snippet258 {

	 public static void main(String[] args) {
	        Display display = new Display();
	        Shell shell = new Shell(display);
	        shell.setLayout(new GridLayout(2, false));
	        
	        final Text text = new Text(shell, SWT.SEARCH | SWT.CANCEL);
	        Image image = null;
	        if ((text.getStyle() & SWT.CANCEL) == 0) {
	            image = new Image (display, Snippet258.class.getResourceAsStream("cancel.gif"));
	            ToolBar toolBar = new ToolBar (shell, SWT.FLAT);
	            ToolItem item = new ToolItem (toolBar, SWT.PUSH);
	            item.setImage (image);
	            item.addSelectionListener(new SelectionAdapter() {
	                public void widgetSelected(SelectionEvent e) {
	                    text.setText("");
	                    System.out.println("Search cancelled");
	                }
	            });
	        }
	        text.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
	        text.setText("Search text");
	        text.addSelectionListener(new SelectionAdapter() {
	            public void widgetDefaultSelected(SelectionEvent e) {
	                if (e.detail == SWT.CANCEL) {
	                    System.out.println("Search cancelled");
	                } else {
	                    System.out.println("Searching for: " + text.getText() + "...");
	                }
	            }
	        });

	        shell.setSize(200,75);
	        shell.open();
	        while (!shell.isDisposed()) {
	            if (!display.readAndDispatch()) display.sleep();
	        }
	        if (image != null) image.dispose();
	        display.dispose();
	    }

}
